import { request, HttpMethod, restartTimes, RequestConfig } from './request'
// BPL相关操作

export const getWeekValue = (date: string) => {
  const config = <RequestConfig>{
    url: `/patient/persureHeartRate/get_week_range`,
    method: HttpMethod.GET,
    data: {
      date: date,
    }
  }
  return request(config); 
}

// 日视图：获取某一天的血压数据（使用daysAgo参数）
export const getChartDay = (data: { patientUid: string, daysAgo: number }) => {
  const config = <RequestConfig>{
    url: `/patient/persureHeartRate/dailyPressureData`,
    method: HttpMethod.GET,
    data: data
  }
  return request(config);
}

// 获取某一天的高压、低压平均值
export const getDailyAvgPressure = (data: Object) => {
  const config = <RequestConfig>{
    url: `/patient/persureHeartRate/dailyAvgPressure`,
    method: HttpMethod.GET,
    data: data
  }
  return request(config);
}

export const getSBPDay = (data: Object) => {
  const config = <RequestConfig>{
    url: `/patient/persureHeartRate/todayMaxMinAvgSystolic`,
    method: HttpMethod.GET,
    data: data
  }
  return request(config);
}

export const getDBPDay = (data: Object) => {
  const config = <RequestConfig>{
    url: `/patient/persureHeartRate/todayMaxMinAvgDiastolic`,
    method: HttpMethod.GET,
    data: data
  }
  return request(config);
}

export const getGapDay = (data: object) => {
  const config = <RequestConfig>{
    url: `/patient/persureHeartRate/todayMaxMinAvgPressureDiff`,
    method: HttpMethod.GET,
    data: data
  }
  return request(config);
}

export const getAbnormalDay = (data: Object) => {
  const config = <RequestConfig>{
    url: `/patient/persureHeartRate/getRiskNum`,
    method: HttpMethod.GET,
    data: data
  }
  return request(config);
}

export const getRecordDay = (data: Object) => {
  const config = <RequestConfig>{
    url: `/patient/persureHeartRate/get_daily_anomaly_stats`,
    method: HttpMethod.GET,
    data: data
  }
  return request(config);
}

// 周视图：获取某一周的血压数据（使用weeksAgo参数）
export const getChartWeek = (data: { patientUid: string, weeksAgo: number }) => {
  const config = <RequestConfig>{
    url: `/patient/persureHeartRate/weeklyPressureData`,
    method: HttpMethod.GET,
    data: data
  }
  return request(config);
}

// 旧版本：使用anyDateInWeek参数（保留兼容性）
export const getChartWeekOld = (data: Object) => {
  const config = <RequestConfig>{
    url: `/patient/persureHeartRate/weeklyAvgPressureByDay`,
    method: HttpMethod.GET,
    data: data
  }
  return request(config);
}

export const getSBPWeek = (data: Object) => {
  const config = <RequestConfig>{
    url: `/patient/persureHeartRate/WeekMaxMinAvgSystolic`,
    method: HttpMethod.GET,
    data: data
  }
  return request(config);
}

export const getDBPWeek = (data: Object) => {
  const config = <RequestConfig>{
    url: `/patient/persureHeartRate/WeekMaxMinAvgDiastolic`,
    method: HttpMethod.GET,
    data: data
  }
  return request(config);
}

export const getGapWeek = (data: object) => {
  const config = <RequestConfig>{
    url: `/patient/persureHeartRate/WeekMaxMinAvgPressureDiff`,
    method: HttpMethod.GET,
    data: data
  }
  return request(config);
}

export const getAbnormalWeek = (data: Object) => {
  const config = <RequestConfig>{
    url: `/patient/persureHeartRate/weekAnomalyCount`,
    method: HttpMethod.GET,
    data: data
  }
  return request(config);
}

export const getRecordWeek = (data: any) => {
  // API需要date参数，如果传入的是anyDateInWeek，需要转换
  const requestData = {
    patientUid: data.patientUid,
    date: data.date || data.anyDateInWeek
  };
  const config = <RequestConfig>{
    url: `/patient/persureHeartRate/get_weekly_anomaly_stats`,
    method: HttpMethod.GET,
    data: requestData
  }
  return request(config);
}

// 月视图：获取某一月的血压数据（使用monthsAgo参数）
export const getChartMonth = (data: { patientUid: string, monthsAgo: number }) => {
  const config = <RequestConfig>{
    url: `/patient/persureHeartRate/monthlyPressureData`,
    method: HttpMethod.GET,
    data: data
  }
  return request(config);
}

// 旧版本：使用anyDateInMonth参数（保留兼容性）
export const getChartMonthOld = (data: Object) => {
  const config = <RequestConfig>{
    url: `/patient/persureHeartRate/monthlyAvgPressureByWeek`,
    method: HttpMethod.GET,
    data: data
  }
  return request(config);
}
export const Heartgrading = (data: { patientUid: string, [key: string]: any }) => {
  const config = <RequestConfig>{
    url: `/patient/persureHeartRate/${data.patientUid}/grading`,  
    method: HttpMethod.GET,
    data: data
  }
  return request(config);
}
export const getSBPMonth = (data: Object) => {
  const config = <RequestConfig>{
    url: `/patient/persureHeartRate/MonthMaxMinAvgSystolic`,
    method: HttpMethod.GET,
    data: data
  }
  return request(config);
}

export const getDBPMonth = (data: Object) => {
  const config = <RequestConfig>{
    url: `/patient/persureHeartRate/MonthMaxMinAvgDiastolic`,
    method: HttpMethod.GET,
    data: data
  }
  return request(config);
}

export const getGapMonth = (data: object) => {
  const config = <RequestConfig>{
    url: `/patient/persureHeartRate/MonthMaxMinAvgPressureDiff`,
    method: HttpMethod.GET,
    data: data
  }
  return request(config);
}

export const getAbnormalMonth = (data: Object) => {
  const config = <RequestConfig>{
    url: `/patient/persureHeartRate/monthAnomalyCount`,
    method: HttpMethod.GET,
    data: data
  }
  return request(config);
}

export const getRecordMonth = (data: Object) => {
  const config = <RequestConfig>{
    url: `/patient/persureHeartRate/get_monthly_anomaly_stats`,
    method: HttpMethod.GET,
    data: data
  }
  return request(config);
}

export const addBPLRecord = (data: Object) => {
  const config = <RequestConfig>{
    url: `/patient/persureHeartRate/add`,
    method: HttpMethod.POST,
    data: data
  }
  return request(config);
}
// 获取血压提醒勿扰时段设置
export const getBPLReminder = () => {
  const patientUid = wx.getStorageSync('patientUid');
  const config = <RequestConfig>{
    url: `/patient/bplReminder/getSettings`,
    method: HttpMethod.GET,
    data: {
      patientUid: patientUid
    }
  }
  return request(config);
}

// 保存血压提醒勿扰时段设置
export const setBPLReminder = (data: Object) => {
  const patientUid = wx.getStorageSync('patientUid');
  const config = <RequestConfig>{
    url: `/patient/bplReminder/saveSettings`,
    method: HttpMethod.POST,
    data: {
      patientUid: patientUid,
      ...data
    }
  }
  return request(config);
}

// ============ 注意：以下接口已废弃，请使用 patientDevice.ts 中的新接口 ============
// 血压自动测量配置现在使用 UserDevice2ConfigController 的接口：
// - getUserAccessToken() - 获取访问令牌
// - switchAutoMeasure() - 开关总控制
// - setDaytimeRange() - 设置白天时间段
// - setDaytimeInterval() - 设置白天间隔
// - setNightInterval() - 设置夜间间隔
// - listUserConfigs() - 查询当前配置
// ============================================================================

/*
// 已废弃：后端没有此接口
export const getBPLAutoMeasureSettings = () => {
  const patientUid = wx.getStorageSync('patientUid');
  const config = <RequestConfig>{
    url: `/patient/bplAutoMeasure/getSettings`,
    method: HttpMethod.GET,
    data: {
      patientUid: patientUid
    }
  }
  return request(config);
}

// 已废弃：后端没有此接口
export const saveBPLAutoMeasureSettings = (data: Object) => {
  const patientUid = wx.getStorageSync('patientUid');
  const config = <RequestConfig>{
    url: `/patient/bplAutoMeasure/saveSettings`,
    method: HttpMethod.POST,
    data: {
      patientUid: patientUid,
      ...data
    }
  }
  return request(config);
}
*/